package com.test;

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

public class MyPageProcessor implements PageProcessor {

    private static String initUrl="http://www.p42u.com/";
    Site site = Site.me()
            .setRetryTimes(3) // 如果失败,重试2次.
            .setSleepTime(1000) // 爬取时,间隔时间.
            .setTimeOut(10000)
            .setUserAgent("Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36");
    public Site getSite() {
        return site;
    }


    public void process(Page page) {
        WebDriver driver = new ChromeDriver();
        driver.get(initUrl);
        try {
            Thread.sleep(5000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        WebElement webElement = ((ChromeDriver) driver).findElementByCssSelector("div#logo img");
        String src = webElement.getAttribute("src");
        System.out.println(src);
    }


    //主程序
    public static void main(String[] args) {
        // FireFox浏览器.
        // System.setProperty("webdriver.gecko.driver","");
        // FireFox浏览器.
        System.setProperty("webdriver.chrome.driver","D:/develop/driver/chromedriver/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get(initUrl);
        try {
            Thread.sleep(10000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        WebElement webElement = ((ChromeDriver) driver).findElementByLinkText("简体中文");
        System.out.println(webElement.getAttribute("href"));
        webElement.click();
        try {
            WebElement id = ((ChromeDriver) driver).findElementById("adultwarningprompt");
            WebElement elementByCssSelector = ((ChromeDriver) driver).findElementByCssSelector("div#adultwarningprompt input.btnAdultAgree");
            elementByCssSelector.click();
        }catch (NoSuchElementException e){
            System.out.println("没有弹出确认框...");
        }
        // 进入了主页面

        Spider.create(new MyPageProcessor())
                .addUrl(initUrl) //设置起始页面
                .addPipeline(new ConsolePipeline()) // 设置输出到控制台
                // .addPipeline(new MyPipline()) // 设置输出到控制台
                .thread(1) // 启动4个线程一起抓取
                .run(); // 启动爬虫
    }

}
